<template>
	<view class="detail">
		<view class="detail_content">
			<view class="info">
				<view class="param">蓝牙Mac</view>
				<view class="vol_txt">{{deviceId}}</view>
			</view>
			<view class="info">
				<view class="param">版本号</view>
				<view class="vol_txt">{{bit15}}.{{bit12_14}}.{{bit8_11}}.{{bit0_7}}</view>
			</view>
			<view class="info">
				<view class="param">电量</view>
				<view class="vol_txt">{{vol}}V</view>
			</view>
		</view>
	</view>

</template>

<script>
	import bleUtils from '@/utils/BleUtils.js';
	import util from '@/utils/util.js';
	export default {
		data() {
			return {
				mac: '',
				deviceId: '',
				vol: 0,
				bit0_7: '',
				bit8_11: '',
				bit12_14: '',
				bit15: ''
			}
		},
		onLoad(option) {
			this.mac = option.mac;
			console.log('mac===>', this.mac);
			this.deviceId = util.strInsert(this.mac, 2);
			this.getBleDevice();
		},
		methods: {
			async getBleDevice() {
				try {
					util.loading('扫描广播');
					await bleUtils.openBluetoothAdapter();
					// 先监听
					uni.onBluetoothDeviceFound((res) => {
						const device = res.devices[0];
						if (!device.name && !device.localName) return;
						const bleName = (device.localName || device.name).trim().toLowerCase();
						console.log('bleName-->', bleName);
						if (bleName.toLowerCase() != this.deviceId.replaceAll(':', '').toLowerCase()) return;
						uni.stopBluetoothDevicesDiscovery({
							success() {
								console.log('蓝牙停止发现')
							}
						})
						console.log('find device-->', device)
						const advertisData = device.advertisData || device.manufacturerData;
						if (!advertisData) return;
						const advertisHex = this.ab2hex(advertisData);
						console.log('found advertisData===>', advertisHex)
						// 将数据进行解析
						const v = advertisHex.substr(4, 2);
						this.vol = (parseInt(v, 16) / 10).toFixed(1);
						// 版本
						const param = parseInt(advertisHex.substr(50, 4), 16);
						/**
						 * Bit0~7   修订版本           0~255
						 * Bit8~11  线序版本引脚线序：  0~15
						 * Bit12~14 平台版本MCU平台：  1~7
						 * Bit15    版本类型           0->正式；1->测试；
						 */
						this.bit0_7 = param & 0x00ff;
						this.bit8_11 = (param & 0x0f00) >> 8;
						this.bit12_14 = (param & (0b111 << 12)) >> 12;
						this.bit15 = (param & (0b1 << 14)) >> 14;
						util.hideLoading();
					})
					// 然后开启搜索
					uni.startBluetoothDevicesDiscovery({
						allowDuplicatesKey: false,
						success: (res) => {
							console.log('startBluetoothDevicesDiscovery success')
						},
						fail: (res) => {
							util.hideLoading();
							util.toast('发现失败!')
						}
					})
				} catch (e) {
					util.hideLoading()
					util.toast('请开启蓝牙并赋予定位权限!');
				}

			},
			// ArrayBuffer转16进度字符串示例
			ab2hex(buffer) {
				const hexArr = Array.prototype.map.call(
					new Uint8Array(buffer),
					function(bit) {
						return ('00' + bit.toString(16)).slice(-2)
					}
				)
				return hexArr.join('');
			}
		}
	}
</script>

<style lang="scss">
	.detail {
		padding: 10px;
		width: calc(100% - 40px);

		.detail_content {
			background: #fff;
			padding: 10px;
			border-radius: 10px;
			height: 160px;
			width: 100%;

			.info {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 40px;
				line-height: 40px;

				.param {
					font-size: 16px;
					font-weight: bold;
					color: dimgray;
				}

				.vol_txt {
					font-size: 14px;
					color: #8c8c8c;
				}
			}
		}

	}
</style>